package com.dmp.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.dmp.util.Global;

public class DBBaseConnect {

	protected static Connection getConnection(String driverName, String fileName, String urlKey, String userKey, String pwdKey) {
		Connection conn = null;
		try {
			initManager(driverName);
			String url = Global.getConfig(fileName, urlKey);
			String user = Global.getConfig(fileName, userKey);
			String password = Global.getConfig(fileName, pwdKey);
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return conn;
	}

	private static void initManager(String driverName) throws Exception {
		Class.forName(driverName);
	}

	public static void release(Connection conn, PreparedStatement ps, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (ps != null) {
				ps.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
